<template>
  <div>
    <span>导出审核报告</span>
  </div>
</template>

<script>
import Docxtemplater from 'docxtemplater'
import PizZip from 'pizzip'
// import JSZip from 'jszip'
import JSZipUtils from 'jszip-utils'
import { saveAs } from 'file-saver'
import axios from 'axios'
import XLSX from 'xlsx'
import { mapGetters } from 'vuex'
import Cookies from "js-cookie";
export default {
  name: 'verifyReport',
  data () {
    return {
      name: '',
      errName: '',
      apply_batch: 0,
    }
  },
  props: {
    // id: {
    //   type: String,
    //   default: ''
    // }
  },
  created () {

    axios.get('/api/ac/maintain/infoProjectService/getCurrentApplyBatch')
      .then(
        response => {
          // 这里写的就是console.log()
          this.apply_batch = response.data.data
          return console.log(response.data)
        })
      .catch(error => error);
    ;
  },
  computed: {

  },
  components: {
    // XLSX
  },
  methods: {
    async word (val) {
      try {


        const res = await this.$http('exportVerify', {
          id: val
        })
        if (!res) {
          return false
        }

        let project = res.projectMap
        let contract = res.contractMaps
        let office = res.officeMap
        let mip_hardMoney = 0
        let mip_hardMoneyVerify = 0
        let mip_sortMoney = 0
        let mip_sortMoneyVerify = 0
        let mip_serveMoney = 0
        let mip_serveMoneyVerify = 0
        let mip_allMoney = 0
        let mip_allMoneyVerify = 0
        // 合同的字段处理
        contract.forEach(item => {
          item.sort = []
          item.hard = []
          item.serve = []
          item.sortMoney = 0
          item.sortMoneyVerify = 0
          item.hardMoney = 0
          item.hardMoneyVerify = 0
          item.serveMoney = 0
          item.serveMoneyVerify = 0
          item.allMoney = 0
          item.allMoneyVerify = 0
          item.deviceMaps.forEach(item1 => {
            if (!item1.third_check_msg) {
              item1.third_check_msg = ' '
            }
            item1.serve_start_date = this.$util.formatDate(item1.serve_start_date)
            item1.serve_end_date = this.$util.formatDate(item1.serve_end_date)
            if (item1.device_type) {
              item1.device_type = this.$util.ITRename(item1.device_type)
              item1.purchase_time = this.$util.formatDate(item1.purchase_time)
              item1.guarantee_time = this.$util.formatDate(item1.guarantee_time)
              if (item1.deviceSubMaps) {
                item1.deviceSubMaps.forEach((item2, index) => {
                  item2.service_method = '服务方式： ' + (item2.service_method == '1' ? '现场' : item2.service_method == '2' ? '远程' : '现场和远程')
                  item2.service_result = '交付成果 ：' + item2.service_result
                  item2.service_time = '服务时间： ' + item2.service_time
                  item2.target_desc = '指标描述： ' + item2.target_desc
                  item2.target_index = '指标项： ' + item2.target_index
                  item2.target_require = '指标要求： ' + item2.target_require
                })
              } else {
              }

              if (item1.device_type.substr(0, 1) == '硬') {
                item.hard.push({
                  ...item1
                })
                if (item1.service_pre_amount) {
                  item.hardMoney = (parseFloat(item.hardMoney) + parseFloat(item1.service_pre_amount.toFixed(4))).toFixed(4)
                }
                if (item1.third_check_amount) {
                  item.hardMoneyVerify = (parseFloat(item.hardMoneyVerify) + parseFloat(item1.third_check_amount.toFixed(4))).toFixed(4)
                }
              } else {
                item.sort.push({
                  ...item1
                })
                if (item1.service_pre_amount) {
                  item.sortMoney = (parseFloat(item.sortMoney) + parseFloat(item1.service_pre_amount.toFixed(4))).toFixed(4)
                }
                if (item1.third_check_amount) {
                  item.sortMoneyVerify = (parseFloat(item.sortMoneyVerify) + parseFloat(item1.third_check_amount.toFixed(4))).toFixed(4)
                }
              }
            } else {
              // item1.target_index = ''
              if (item1.deviceSubMaps) {
                item1.deviceSubMaps.forEach((item2, index) => {
                  // item1.target_index = item1.target_index + ' ' + '第' + (index + 1) + '条' + '、 ' + item2.target_index
                  item2.service_method = '服务方式： ' + (item2.service_method == '1' ? '现场' : item2.service_method == '2' ? '远程' : '现场和远程')
                  item2.service_result = '交付成果： ' + item2.service_result
                  item2.service_time = '服务时间： ' + item2.service_time
                  item2.target_desc = '指标描述： ' + item2.target_desc
                  item2.target_index = '指标项： ' + item2.target_index
                  item2.target_require = '指标要求： ' + item2.target_require
                })
              } else {
                item1.target_index = '未填写'
              }
              item1.service_type = this.$util.ITRename(item1.service_type)
              item.serve.push({
                ...item1
              })
              if (item1.service_pre_amount) {
                item.serveMoney = (parseFloat(item.serveMoney) + parseFloat(item1.service_pre_amount.toFixed(4))).toFixed(4)
              }
              if (item1.third_check_amount) {
                item.serveMoneyVerify = (parseFloat(item.serveMoneyVerify) + parseFloat(item1.third_check_amount.toFixed(4))).toFixed(4)
              }
            }
          })
          item.allMoney = (parseFloat(item.allMoney) + parseFloat(item.sortMoney) + parseFloat(item.hardMoney) + parseFloat(item.serveMoney)).toFixed(4)
          item.allMoneyVerify = (parseFloat(item.allMoneyVerify) + parseFloat(item.sortMoneyVerify) + parseFloat(item.hardMoneyVerify) + parseFloat(item.serveMoneyVerify)).toFixed(4)
        })

        contract.forEach(item => {
          mip_hardMoney = (parseFloat(mip_hardMoney) + parseFloat(item.hardMoney)).toFixed(4)
          mip_hardMoneyVerify = (parseFloat(mip_hardMoneyVerify) + parseFloat(item.hardMoneyVerify)).toFixed(4)
          mip_sortMoney = (parseFloat(mip_sortMoney) + parseFloat(item.sortMoney)).toFixed(4)
          mip_sortMoneyVerify = (parseFloat(mip_sortMoneyVerify) + parseFloat(item.sortMoneyVerify)).toFixed(4)
          mip_serveMoney = (parseFloat(mip_serveMoney) + parseFloat(item.serveMoney)).toFixed(4)
          mip_serveMoneyVerify = (parseFloat(mip_serveMoneyVerify) + parseFloat(item.serveMoneyVerify)).toFixed(4)
          mip_allMoney = (parseFloat(mip_allMoney) + parseFloat(item.allMoney)).toFixed(4)
          mip_allMoneyVerify = (parseFloat(mip_allMoneyVerify) + parseFloat(item.allMoneyVerify)).toFixed(4)
          item.hard.forEach((item1, index) => {
            if (item1.deviceSubMaps) {
              item1.deviceSubMaps.forEach((item1_1, index) => {
                item1_1.index = '第' + (index + 1) + '条：'
              })
            }
            item1.index = index + 1
            item1.purchase_price = item1.purchase_price.toFixed(4)
            item1.service_pre_amount = item1.service_pre_amount.toFixed(4)
            item1.third_check_amount = item1.third_check_amount.toFixed(4)
          })
          item.sort.forEach((item2, index) => {
            if (item2.deviceSubMaps) {
              item2.deviceSubMaps.forEach((item2_1, index) => {
                item2_1.index = '第' + (index + 1) + '条：'
              })
            }
            item2.index = index + 1
            item2.purchase_price = item2.purchase_price.toFixed(4)
            item2.service_pre_amount = item2.service_pre_amount.toFixed(4)
            item2.third_check_amount = item2.third_check_amount.toFixed(4)
          })
          item.serve.forEach((item3, index) => {
            if (item3.deviceSubMaps) {
              item3.deviceSubMaps.forEach((item3_1, index) => {
                item3_1.index = '第' + (index + 1) + '条：'
              })
            }
            item3.index = index + 1
            // item3.buy_type = item3.buy_type == '1' ? '单一来源' : '公开招标'
            item3.service_pre_amount = item3.service_pre_amount.toFixed(4)
            item3.third_check_amount = item3.third_check_amount.toFixed(4)
          })
        })
        // 需要的字段构建
        this.errName = office.name + '-' + project.project_name
        let mip_project_name = project.project_name// 项目名称
        let mip_maintenance_contacts = project.maintenance_contacts// 联系人
        let mip_maintenance_contacts_mobile = project.maintenance_contacts_mobile// 联系电话
        let mip_create_time = this.$util.formatDate(project.create_date)// 项目创建时间
        // let mip_year = this.$util.formatDate(new Date(), 'yyyy')// 本年度时间
        let mip_year = this.apply_batch;
        let mip_third_check_msg = project.third_check_msg ? project.third_check_msg : ' '// 三方咨询机构意见
        // let mpci_start_time = contract.start_time// 运维开始时间
        let mip_update_by = this.$util.formatDate(project.update_date)// 运维结束时间
        let mip_contacts = project.contacts// 项目负责人
        let mip_contact_mobile = project.contact_mobile// 项目负责人电话
        let mso_name = office.name
        let mip_project_code = project.project_code // 项目编号
        this.name = office.name + '-' + mip_project_name
        let obj = {
          mso_name,
          mip_project_name,
          mip_maintenance_contacts,
          mip_maintenance_contacts_mobile,
          mip_create_time,
          mip_year,
          mip_contacts,
          mip_contact_mobile,
          contract,
          mip_third_check_msg,
          mip_update_by,
          mip_hardMoney,
          mip_hardMoneyVerify,
          mip_sortMoney,
          mip_sortMoneyVerify,
          mip_serveMoney,
          mip_serveMoneyVerify,
          mip_allMoney,
          mip_allMoneyVerify,
          mip_project_code
        }
        // let pdfUrl = window.URL.createObjectURL(
        //   new Blob(obj, { type: "application/pdf" }))

        // window.open(pdfUrl)
        this.makeWord(obj)
      } catch (err) {
        this.$tips.error(this.errName + '导出失败' + err.msg,)
      }
    },
    makeWord (item) {
      console.log(this.$util.formatDate(new Date(), 'yyyy'));
      // let ImageModule = require('docxtemplater-image-module-free')
      JSZipUtils.getBinaryContent('/static/doc/verify-report.docx', (error, content) => {
        // verify-report.docx是模板。我们在导出的时候，会根据此模板来导出对应的数据
        // 抛出异常
        if (error) {
          throw error
        }

        // // 创建一个PizZip实例，内容为模板的内容
        // let zip = new PizZip(content)
        // // 创建并加载docxtemplater实例对象
        // let doc = new Docxtemplater()
        // doc.loadZip(zip)
        // // 设置模板变量的值
        // doc.setData(item)
        // 创建一个PizZip实例，内容为模板的内容
        let zip = new PizZip(content)
        // 创建并加载docxtemplater实例对象
        let doc = new Docxtemplater(zip, { linebreaks: true }, { paragraphLoop: true })
        // linebreaks 开启换行  paragraphLoop  清除多余间隔
        // 设置模板变量的值
        doc.setData(item)

        try {
          // 用模板变量的值替换所有模板变量
          doc.render()
        } catch (error) {
          // 抛出异常
          let e = {
            message: error.message,
            name: error.name,
            stack: error.stack,
            properties: error.properties
          }
          throw error
        }
        // 生成一个代表docxtemplater对象的zip文件（不是一个真实的文件，而是在内存中的表示）
        let out = doc.getZip().generate({
          type: 'blob',
          mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        })
        // 将目标文件对象保存为目标类型的文件，并命名
        saveAs(out, this.name + '.docx')
      })
    },
  }
}
</script>

<style scoped>
</style>
